home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11667 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  96 lines

  1. Path: abacus.abasoft.co.uk!not-for-mail
  2. From: dmb@abacus.abasoft.co.uk (David Byrne)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Major problem with strings.
  5. Date: 14 Mar 1996 10:52:43 -0000
  6. Organization: Abacus Software Ltd.
  7. Message-ID: <4i8tpr$1he@abacus.abasoft.co.uk>
  8. References: <31438275.72DB@aol2.com> <4i0gn0$5g9@sam.inforamp.net>
  9. NNTP-Posting-Host: abacus.abasoft.co.uk
  10. X-NNTP-Posting-Host: abacus.demon.co.uk
  11.  
  12. In article <4i0gn0$5g9@sam.inforamp.net>,
  13. Randy Charles Morin <rmorin@inforamp.net> wrote:
  14. >In article <31438275.72DB@aol2.com>, Neil <neil@aol2.com> wrote:
  15. [code snipped]
  16. >No, this just won't do.  When you want a '/' slash in C, use two slashes.  The 
  17. >'/' is also used to denote an escape sequence (for special characters).  Thus 
  18. >your code should read...
  19. >
  20. >1    char *club="";
  21. >2    club="//public_html//neil";
  22. >3    strcat(club,argv[1]+5);
  23. >4    strcat(club,"//");
  24.  
  25. This is a joke, right ? 
  26.  
  27. Neil: line 1: club is declared as a char*
  28.       line 2: an area of memory is initialised with the string
  29.         "/public_html/neil" (The '/' characters are correct, '\' is the escape
  30.         char which you need to double up to get a genuine backslash)
  31.       line 3: all characters from the sixth character of the first argument up 
  32.         to the next null character are copied over whatever data is in memory
  33.         after the area of memory allocated in line 2 (A *BAD* thing !)
  34.       line 4: more data trashing
  35.  
  36. Try something like:
  37.  
  38. ...
  39.  
  40. // work out a suitable size depending on what your longest argument is
  41. const int MAX_ARG_LEN=20;
  42. const char* base_dir="/public_html/neil";
  43.  
  44. int main(int argc, char **argv)
  45. {
  46.     char *club=new char[strlen(base_dir) + MAX_ARG_LEN + 1];
  47.  
  48.     if (argc > 1)
  49.     {
  50.         // put some sort of check here to ensure we only copy MAX_ARG_LEN chars
  51.         // or use strncat with appropriate args etc.
  52.         strcat(club, argv[1]+5);
  53.         strcat(club,"/");
  54.     }
  55.  
  56.     // etc. etc.
  57.  
  58.     delete[] club;
  59. }
  60.  
  61.  
  62. I think a nicer way to do this is:
  63.  
  64. #include <iostream.h>
  65. #include <strstream.h>
  66. #include <string.h>
  67.  
  68. const char* base_dir="/public_html/neil";
  69.  
  70. int main(int argc, char **argv)
  71. {
  72.     ostrstream os;
  73.  
  74.     os << base_dir;
  75.     if (argc > 1)
  76.         os << argv[1]+5 << "/";
  77.  
  78.     char *club=os.str();
  79.  
  80.     // use club for whatever purposes
  81.     cout << club << endl;
  82.  
  83.     delete[] club;
  84.     // etc. etc.
  85. }
  86.  
  87. You could also consider using a string class or STL vector<char> to store the
  88. data in as alternatives.
  89.  
  90. David
  91. -- 
  92. David Byrne, Abacus Software, London, UK              Tel: +44 (0)171 603 9877
  93. Email: dmb@abacus.demon.co.uk                         Fax: +44 (0)171 603 6844
  94. Here's a koan: If you have ice-cream I will give you some. If you have none,
  95.                I will take it away from you. (it's an ice-cream koan).
  96.